package com.ctrl.riveradministrator;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

import com.baidu.mapapi.SDKInitializer;
import com.ctrl.riveradministrator.common.Constants;
import com.ctrl.riveradministrator.db.DaoMaster;
import com.ctrl.riveradministrator.db.DaoSession;
import com.ctrl.riveradministrator.util.CommonUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.tencent.bugly.crashreport.CrashReport;

public class RaApplication extends Application {

    public static RaApplication raApplication;
    public static Resources resources;
    public static SharedPreferences sp;
    public static SharedPreferences.Editor editor;
    private DaoSession daoSession;
    private SQLiteDatabase db;

    public static int screenWidth = 0;

    public static int screenHeight = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        raApplication = this;
        resources = this.getResources();
        sp = getSharedPreferences(Constants.GLOBAL_SP, MODE_PRIVATE);
        editor = sp.edit();
        Fresco.initialize(this);
        SDKInitializer.setAgreePrivacy(this,true);
        SDKInitializer.initialize(this);
        initGreenDao();
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                Log.e("-------------", activity.getLocalClassName());
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });

        initBugly();
    }

    private void initBugly() {
        Context context = getApplicationContext();
// 获取当前包名
        String packageName = context.getPackageName();
// 获取当前进程名
        String processName = CommonUtil.getProcessName(android.os.Process.myPid());
// 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
// 初始化Bugly
        CrashReport.initCrashReport(context, "2bad402ee6", BuildConfig.DEBUG, strategy);
    }

    /**
     * 获取当前人员ID
     *
     * @return id
     */
    public static String getUserId() {
        if (sp.getString(Constants.USER_ID, null) == null) {
            return null;
        }
        return sp.getString(Constants.USER_ID, null);
    }

    private void initGreenDao() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "trace.db");
        db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

}
